home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / Make / source / file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-27  |  15.8 KB  |  670 lines

  1. /* Target file hash table management for GNU Make.
  2. Copyright (C) 1988,89,90,91,92,93,94,95,96,97 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "dep.h"
  21. #include "filedef.h"
  22. #include "job.h"
  23. #include "commands.h"
  24. #include "variable.h"
  25. #include <assert.h>
  26.  
  27.  
  28. /* Hash table of files the makefile knows how to make.  */
  29.  
  30. #ifndef    FILE_BUCKETS
  31. #define FILE_BUCKETS    1007
  32. #endif
  33. static struct file *files[FILE_BUCKETS];
  34.  
  35. /* Number of files with the `intermediate' flag set.  */
  36.  
  37. unsigned int num_intermediates = 0;
  38.  
  39.  
  40. /* Access the hash table of all file records.
  41.    lookup_file  given a name, return the struct file * for that name,
  42.            or nil if there is none.
  43.    enter_file   similar, but create one if there is none.  */
  44.  
  45. struct file *
  46. lookup_file (name)
  47.      char *name;
  48. {
  49.   register struct file *f;
  50.   register char *n;
  51.   register unsigned int hashval;
  52.  
  53.   if (*name == '\0')
  54.     abort ();
  55.  
  56.   /* This is also done in parse_file_seq, so this is redundant
  57.      for names read from makefiles.  It is here for names passed
  58.      on the command line.  */
  59. #ifdef VMS
  60.   while (name[0] == '[' && name[1] == ']' && name[2] != '\0')
  61.       name += 2;
  62. #endif
  63.   while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
  64.     {
  65.       name += 2;
  66.       while (*name == '/')
  67.     /* Skip following slashes: ".//foo" is "foo", not "/foo".  */
  68.     ++name;
  69.     }
  70.  
  71.   if (*name == '\0')
  72.     /* It was all slashes after a dot.  */
  73. #ifdef VMS
  74.     name = "[]";
  75. #else
  76. #ifdef _AMIGA
  77.     name = "";
  78. #else
  79.     name = "./";
  80. #endif /* AMIGA */
  81. #endif /* VMS */
  82.  
  83.   hashval = 0;
  84.   for (n = name; *n != '\0'; ++n)
  85.     HASHI (hashval, *n);
  86.   hashval %= FILE_BUCKETS;
  87.  
  88.   for (f = files[hashval]; f != 0; f = f->next)
  89.     {
  90.       if (strieq (f->hname, name))
  91.     {
  92.       return f;
  93.     }
  94.     }
  95.   return 0;
  96. }
  97.  
  98. struct file *
  99. enter_file (name)
  100.      char *name;
  101. {
  102.   register struct file *f, *new;
  103.   register char *n;
  104.   register unsigned int hashval;
  105. #ifdef VMS
  106.   char *lname, *ln;
  107. #endif
  108.  
  109.   if (*name == '\0')
  110.     abort ();
  111.  
  112. #ifdef VMS
  113.   lname = (char *)malloc (strlen (name) + 1);
  114.   for (n = name, ln = lname; *n != '\0'; ++n, ++ln)
  115.     {
  116.       if (isupper(*n))
  117.     *ln = tolower(*n);
  118.       else
  119.     *ln = *n;
  120.     }
  121.   *ln = 0;
  122.   name = lname;
  123. #endif
  124.  
  125.   hashval = 0;
  126.   for (n = name; *n != '\0'; ++n)
  127.     HASHI (hashval, *n);
  128.   hashval %= FILE_BUCKETS;
  129.  
  130.   for (f = files[hashval]; f != 0; f = f->next)
  131.     if (strieq (f->hname, name))
  132.       break;
  133.  
  134.   if (f != 0 && !f->double_colon)
  135.     {
  136. #ifdef VMS
  137.       free(lname);
  138. #endif
  139.       return f;
  140.     }
  141.  
  142.   new = (struct file *) xmalloc (sizeof (struct file));
  143.   bzero ((char *) new, sizeof (struct file));
  144.   new->name = new->hname = name;
  145.   new->update_status = -1;
  146.  
  147.   if (f == 0)
  148.     {
  149.       /* This is a completely new file.  */
  150.       new->next = files[hashval];
  151.       files[hashval] = new;
  152.     }
  153.   else
  154.     {
  155.       /* There is already a double-colon entry for this file.  */
  156.       new->double_colon = f;
  157.       while (f->prev != 0)
  158.     f = f->prev;
  159.       f->prev = new;
  160.     }
  161.  
  162.   return new;
  163. }
  164.  
  165. /* Rehash FILE to NAME.  This is not as simple as resetting
  166.    the `hname' member, since it must be put in a new hash bucket,
  167.    and possibly merged with an existing file called NAME.  */
  168.  
  169. void
  170. rehash_file (file, name)
  171.      register struct file *file;
  172.      char *name;
  173. {
  174.   char *oldname = file->hname;
  175.   register unsigned int oldhash;
  176.   register char *n;
  177.  
  178.   while (file->renamed != 0)
  179.     file = file->renamed;
  180.  
  181.   /* Find the hash values of the old and new names.  */
  182.  
  183.   oldhash = 0;
  184.   for (n = oldname; *n != '\0'; ++n)
  185.     HASHI (oldhash, *n);
  186.  
  187.   file_hash_enter (file, name, oldhash, file->name);
  188. }
  189.  
  190. /* Rename FILE to NAME.  This is not as simple as resetting
  191.    the `name' member, since it must be put in a new hash bucket,
  192.    and possibly merged with an existing file called NAME.  */
  193.  
  194. void
  195. rename_file (file, name)
  196.      register struct file *file;
  197.      char *name;
  198. {
  199.   rehash_file(file, name);
  200.   while (file)
  201.     {
  202.       file->name = file->hname;
  203.       file = file->prev;
  204.     }
  205. }
  206.  
  207. void
  208. file_hash_enter (file, name, oldhash, oldname)
  209.      register struct file *file;
  210.      char *name;
  211.      unsigned int oldhash;
  212.      char *oldname;
  213. {
  214.   unsigned int oldbucket = oldhash % FILE_BUCKETS;
  215.   register unsigned int newhash, newbucket;
  216.   struct file *oldfile;
  217.   register char *n;
  218.   register struct file *f;
  219.  
  220.   newhash = 0;
  221.   for (n = name; *n != '\0'; ++n)
  222.     HASHI (newhash, *n);
  223.   newbucket = newhash % FILE_BUCKETS;
  224.  
  225.   /* Look for an existing file under the new name.  */
  226.  
  227.   for (oldfile = files[newbucket]; oldfile != 0; oldfile = oldfile->next)
  228.     if (strieq (oldfile->hname, name))
  229.       break;
  230.  
  231.   /* If the old file is the same as the new file, something's wrong.  */
  232.   assert (oldfile != file);
  233.  
  234.   if (oldhash != 0 && (newbucket != oldbucket || oldfile != 0))
  235.     {
  236.       /* Remove FILE from its hash bucket.  */
  237.  
  238.       struct file *lastf = 0;
  239.  
  240.       for (f = files[oldbucket]; f != file; f = f->next)
  241.     lastf = f;
  242.  
  243.       if (lastf == 0)
  244.     files[oldbucket] = f->next;
  245.       else
  246.     lastf->next = f->next;
  247.     }
  248.  
  249.   /* Give FILE its new name.  */
  250.  
  251.   file->hname = name;
  252.   for (f = file->double_colon; f != 0; f = f->prev)
  253.     f->hname = name;
  254.  
  255.   if (oldfile == 0)
  256.     {
  257.       /* There is no existing file with the new name.  */
  258.  
  259.       if (newbucket != oldbucket)
  260.     {
  261.       /* Put FILE in its new hash bucket.  */
  262.       file->next = files[newbucket];
  263.       files[newbucket] = file;
  264.     }
  265.     }
  266.   else
  267.     {
  268.       /* There is an existing file with the new name.
  269.      We must merge FILE into the existing file.  */
  270.  
  271.       register struct dep *d;
  272.  
  273.       if (file->cmds != 0)
  274.     {
  275.       if (oldfile->cmds == 0)
  276.         oldfile->cmds = file->cmds;
  277.       else if (file->cmds != oldfile->cmds)
  278.         {
  279.           /* We have two sets of commands.  We will go with the
  280.          one given in the rule explicitly mentioning this name,
  281.          but give a message to let the user know what's going on.  */
  282.           if (oldfile->cmds->filename != 0)
  283.         makefile_error (file->cmds->filename, file->cmds->lineno,
  284.                 "Commands were specified for \
  285. file `%s' at %s:%u,",
  286.                 oldname, oldfile->cmds->filename,
  287.                 oldfile->cmds->lineno);
  288.           else
  289.         makefile_error (file->cmds->filename, file->cmds->lineno,
  290.                 "Commands for file `%s' were found by \
  291. implicit rule search,",
  292.                 oldname);
  293.           makefile_error (file->cmds->filename, file->cmds->lineno,
  294.                   "but `%s' is now considered the same file \
  295. as `%s'.",
  296.                   oldname, name);
  297.           makefile_error (file->cmds->filename, file->cmds->lineno,
  298.                   "Commands for `%s' will be ignored \
  299. in favor of those for `%s'.",
  300.                   name, oldname);
  301.         }
  302.     }
  303.  
  304.       /* Merge the dependencies of the two files.  */
  305.  
  306.       d = oldfile->deps;
  307.       if (d == 0)
  308.     oldfile->deps = file->deps;
  309.       else
  310.     {
  311.       while (d->next != 0)
  312.         d = d->next;
  313.       d->next = file->deps;
  314.     }
  315.  
  316.       merge_variable_set_lists (&oldfile->variables, file->variables);
  317.  
  318.       if (oldfile->double_colon && file->is_target && !file->double_colon)
  319.     fatal ("can't rename single-colon `%s' to double-colon `%s'",
  320.            oldname, name);
  321.       if (!oldfile->double_colon  && file->double_colon)
  322.     {
  323.       if (oldfile->is_target)
  324.         fatal ("can't rename double-colon `%s' to single-colon `%s'",
  325.            oldname, name);
  326.       else
  327.         oldfile->double_colon = file->double_colon;
  328.     }
  329.  
  330.       if (file->last_mtime > oldfile->last_mtime)
  331.     /* %%% Kludge so -W wins on a file that gets vpathized.  */
  332.     oldfile->last_mtime = file->last_mtime;
  333.  
  334. #define MERGE(field) oldfile->field |= file->field
  335.       MERGE (precious);
  336.       MERGE (tried_implicit);
  337.       MERGE (updating);
  338.       MERGE (updated);
  339.       MERGE (is_target);
  340.       MERGE (cmd_target);
  341.       MERGE (phony);
  342.       MERGE (ignore_vpath);
  343. #undef MERGE
  344.  
  345.       file->renamed = oldfile;
  346.     }
  347. }
  348.  
  349. /* Remove all nonprecious intermediate files.
  350.    If SIG is nonzero, this was caused by a fatal signal,
  351.    meaning that a different message will be printed, and
  352.    the message will go to stderr rather than stdout.  */
  353.  
  354. void
  355. remove_intermediates (sig)
  356.      int sig;
  357. {
  358.   register int i;
  359.   register struct file *f;
  360.   char doneany;
  361.  
  362.   if (question_flag || touch_flag)
  363.     return;
  364.   if (sig && just_print_flag)
  365.     return;
  366.  
  367.   doneany = 0;
  368.   for (i = 0; i < FILE_BUCKETS; ++i)
  369.     for (f = files[i]; f != 0; f = f->next)
  370.       if (f->intermediate && (f->dontcare || !f->precious)
  371.       && !f->secondary)
  372.     {
  373.       int status;
  374.       if (f->update_status == -1)
  375.         /* If nothing would have created this file yet,
  376.            don't print an "rm" command for it.  */
  377.         continue;
  378.       else if (just_print_flag)
  379.         status = 0;
  380.       else
  381.         {
  382.           status = unlink (f->name);
  383.           if (status < 0 && errno == ENOENT)
  384.         continue;
  385.         }
  386.       if (!f->dontcare)
  387.         {
  388.           if (sig)
  389.         error ("*** Deleting intermediate file `%s'", f->name);
  390.           else if (!silent_flag)
  391.         {
  392.           if (! doneany)
  393.             {
  394.               fputs ("rm ", stdout);
  395.               doneany = 1;
  396.             }
  397.           else
  398.             putchar (' ');
  399.           fputs (f->name, stdout);
  400.           fflush (stdout);
  401.         }
  402.           if (status < 0)
  403.         perror_with_name ("unlink: ", f->name);
  404.         }
  405.     }
  406.  
  407.   if (doneany && !sig)
  408.     {
  409.       putchar ('\n');
  410.       fflush (stdout);
  411.     }
  412. }
  413.  
  414. /* For each dependency of each file, make the `struct dep' point
  415.    at the appropriate `struct file' (which may have to be created).
  416.  
  417.    Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
  418.    and various other special targets.  */
  419.  
  420. void
  421. snap_deps ()
  422. {
  423.   register struct file *f, *f2;
  424.   register struct dep *d;
  425.   register int i;
  426.  
  427.   /* Enter each dependency name as a file.  */
  428.   for (i = 0; i < FILE_BUCKETS; ++i)
  429.     for (f = files[i]; f != 0; f = f->next)
  430.       for (f2 = f; f2 != 0; f2 = f2->prev)
  431.     for (d = f2->deps; d != 0; d = d->next)
  432.       if (d->name != 0)
  433.         {
  434.           d->file = lookup_file (d->name);
  435.           if (d->file == 0)
  436.         d->file = enter_file (d->name);
  437.           else
  438.         free (d->name);
  439.           d->name = 0;
  440.         }
  441.  
  442.   for (f = lookup_file (".PRECIOUS"); f != 0; f = f->prev)
  443.     for (d = f->deps; d != 0; d = d->next)
  444.       for (f2 = d->file; f2 != 0; f2 = f2->prev)
  445.     f2->precious = 1;
  446.  
  447.   for (f = lookup_file (".PHONY"); f != 0; f = f->prev)
  448.     for (d = f->deps; d != 0; d = d->next)
  449.       for (f2 = d->file; f2 != 0; f2 = f2->prev)
  450.     {
  451.       /* Mark this file as phony and nonexistent.  */
  452.       f2->phony = 1;
  453.       f2->last_mtime = (time_t) -1;
  454.     }
  455.  
  456.   for (f = lookup_file (".INTERMEDIATE"); f != 0; f = f->prev)
  457.     {
  458.       /* .INTERMEDIATE with deps listed
  459.      marks those deps as intermediate files.  */
  460.       for (d = f->deps; d != 0; d = d->next)
  461.     for (f2 = d->file; f2 != 0; f2 = f2->prev)
  462.       f2->intermediate = 1;
  463.       /* .INTERMEDIATE with no deps does nothing.
  464.      Marking all files as intermediates is useless
  465.      since the goal targets would be deleted after they are built.  */
  466.     }
  467.  
  468.   for (f = lookup_file (".SECONDARY"); f != 0; f = f->prev)
  469.     {
  470.       /* .SECONDARY with deps listed
  471.      marks those deps as intermediate files
  472.      in that they don't get rebuilt if not actually needed;
  473.      but unlike real intermediate files,
  474.      these are not deleted after make finishes.  */
  475.       if (f->deps)
  476.     {
  477.       for (d = f->deps; d != 0; d = d->next)
  478.         for (f2 = d->file; f2 != 0; f2 = f2->prev)
  479.           f2->intermediate = f2->secondary = 1;
  480.     }
  481.       /* .SECONDARY with no deps listed marks *all* files that way.  */
  482.       else
  483.     {
  484.       int i;
  485.       for (i = 0; i < FILE_BUCKETS; i++)
  486.         for (f2 = files[i]; f2; f2= f2->next)
  487.           f2->intermediate = f2->secondary = 1;
  488.     }
  489.     }
  490.  
  491.   f = lookup_file (".EXPORT_ALL_VARIABLES");
  492.   if (f != 0 && f->is_target)
  493.     export_all_variables = 1;
  494.  
  495.   f = lookup_file (".IGNORE");
  496.   if (f != 0 && f->is_target)
  497.     {
  498.       if (f->deps == 0)
  499.     ignore_errors_flag = 1;
  500.       else
  501.     for (d = f->deps; d != 0; d = d->next)
  502.       for (f2 = d->file; f2 != 0; f2 = f2->prev)
  503.         f2->command_flags |= COMMANDS_NOERROR;
  504.     }
  505.  
  506.   f = lookup_file (".SILENT");
  507.   if (f != 0 && f->is_target)
  508.     {
  509.       if (f->deps == 0)
  510.     silent_flag = 1;
  511.       else
  512.     for (d = f->deps; d != 0; d = d->next)
  513.       for (f2 = d->file; f2 != 0; f2 = f2->prev)
  514.         f2->command_flags |= COMMANDS_SILENT;
  515.     }
  516.  
  517.   f = lookup_file (".POSIX");
  518.   if (f != 0 && f->is_target)
  519.     posix_pedantic = 1;
  520. }
  521.  
  522. /* Set the `command_state' member of FILE and all its `also_make's.  */
  523.  
  524. void
  525. set_command_state (file, state)
  526.      struct file *file;
  527.      int state;
  528. {
  529.   struct dep *d;
  530.  
  531.   file->command_state = state;
  532.  
  533.   for (d = file->also_make; d != 0; d = d->next)
  534.     d->file->command_state = state;
  535. }
  536.  
  537. /* Print the data base of files.  */
  538.  
  539. static void
  540. print_file (f)
  541.      struct file *f;
  542. {
  543.   register struct dep *d;
  544.  
  545.   putchar ('\n');
  546.   if (!f->is_target)
  547.     puts ("# Not a target:");
  548.   printf ("%s:%s", f->name, f->double_colon ? ":" : "");
  549.  
  550.   for (d = f->deps; d != 0; d = d->next)
  551.     printf (" %s", dep_name (d));
  552.   putchar ('\n');
  553.  
  554.   if (f->precious)
  555.     puts ("#  Precious file (dependency of .PRECIOUS).");
  556.   if (f->phony)
  557.     puts ("#  Phony target (dependency of .PHONY).");
  558.   if (f->cmd_target)
  559.     puts ("#  Command-line target.");
  560.   if (f->dontcare)
  561.     puts ("#  A default or MAKEFILES makefile.");
  562.   printf ("#  Implicit rule search has%s been done.\n",
  563.       f->tried_implicit ? "" : " not");
  564.   if (f->stem != 0)
  565.     printf ("#  Implicit/static pattern stem: `%s'\n", f->stem);
  566.   if (f->intermediate)
  567.     puts ("#  File is an intermediate dependency.");
  568.   if (f->also_make != 0)
  569.     {
  570.       fputs ("#  Also makes:", stdout);
  571.       for (d = f->also_make; d != 0; d = d->next)
  572.     printf (" %s", dep_name (d));
  573.       putchar ('\n');
  574.     }
  575.   if (f->last_mtime == (time_t) 0)
  576.     puts ("#  Modification time never checked.");
  577.   else if (f->last_mtime == (time_t) -1)
  578.     puts ("#  File does not exist.");
  579.   else
  580.     printf ("#  Last modified %.24s (%ld)\n",
  581.         ctime (&f->last_mtime), (long int) f->last_mtime);
  582.   printf ("#  File has%s been updated.\n",
  583.       f->updated ? "" : " not");
  584.   switch (f->command_state)
  585.     {
  586.     case cs_running:
  587.       puts ("#  Commands currently running (THIS IS A BUG).");
  588.       break;
  589.     case cs_deps_running:
  590.       puts ("#  Dependencies commands running (THIS IS A BUG).");
  591.       break;
  592.     case cs_not_started:
  593.     case cs_finished:
  594.       switch (f->update_status)
  595.     {
  596.     case -1:
  597.       break;
  598.     case 0:
  599.       puts ("#  Successfully updated.");
  600.       break;
  601.     case 1:
  602.       assert (question_flag);
  603.       puts ("#  Needs to be updated (-q is set).");
  604.       break;
  605.     case 2:
  606.       puts ("#  Failed to be updated.");
  607.       break;
  608.     default:
  609.       puts ("#  Invalid value in `update_status' member!");
  610.       fflush (stdout);
  611.       fflush (stderr);
  612.       abort ();
  613.     }
  614.       break;
  615.     default:
  616.       puts ("#  Invalid value in `command_state' member!");
  617.       fflush (stdout);
  618.       fflush (stderr);
  619.       abort ();
  620.     }
  621.  
  622.   if (f->variables != 0)
  623.     print_file_variables (f);
  624.  
  625.   if (f->cmds != 0)
  626.     print_commands (f->cmds);
  627. }
  628.  
  629. void
  630. print_file_data_base ()
  631. {
  632.   register unsigned int i, nfiles, per_bucket;
  633.   register struct file *file;
  634.  
  635.   puts ("\n# Files");
  636.  
  637.   per_bucket = nfiles = 0;
  638.   for (i = 0; i < FILE_BUCKETS; ++i)
  639.     {
  640.       register unsigned int this_bucket = 0;
  641.  
  642.       for (file = files[i]; file != 0; file = file->next)
  643.     {
  644.       register struct file *f;
  645.  
  646.       ++this_bucket;
  647.  
  648.       for (f = file; f != 0; f = f->prev)
  649.         print_file (f);
  650.     }
  651.  
  652.       nfiles += this_bucket;
  653.       if (this_bucket > per_bucket)
  654.     per_bucket = this_bucket;
  655.     }
  656.  
  657.   if (nfiles == 0)
  658.     puts ("\n# No files.");
  659.   else
  660.     {
  661.       printf ("\n# %u files in %u hash buckets.\n", nfiles, FILE_BUCKETS);
  662. #ifndef    NO_FLOAT
  663.       printf ("# average %.1f files per bucket, max %u files in one bucket.\n",
  664.           ((double) nfiles) / ((double) FILE_BUCKETS) * 100.0, per_bucket);
  665. #endif
  666.     }
  667. }
  668.  
  669. /* EOF */
  670.